from django.test import TestCase

from .models import Cate, Goods

import random


# # 1. 插入分类测试数据
# types_list = ['新鲜水果', '海鲜水产', '猪牛羊肉', '禽类蛋品', '新鲜蔬菜', '速冻食品']
#
# for name in types_list:
#     # 1. 实例化
#     tp = Cate()
#     # 2.赋值
#     tp.name = name
#     tp.save()


# 2. 添加商品
# 单位
units = ['500克', '1吨', '2个', '3条', '1包', '5支', '1头', '2瓶', '1套', '1枚']


# 2. 插入商品测试数据
with open(r'C:\Users\bq07140\Desktop\haoyouduo-2004-b\hyd_django\data.txt', 'r', encoding='utf-8') as f:

    for name in f:
        # name = line[:-2]  # 把最后一个换行符切掉
        # print(line)
        # print(name + '--------')

        price = random.randint(10, 1000)  # 随机取价格

        # 1. 商品实例
        goods = Goods()

        # 2. 赋值
        goods.name = name
        goods.price = price*0.8
        goods.img = 'static/goods/%s.jpg' % random.randint(1, 30)
        goods.desc = name*5  # 简介
        goods.unit = random.choice(units)  # 单位
        goods.detail = name*20  # 详情
        goods.cate_id = random.randint(1, 6)  # 所属分类id
        # 3. 保存
        goods.save()









